//
//  FindTableViewController.m
//  Car
//
//  Created by lanou on 15/9/17.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "DiscoverViewController.h"
#import "CarRadioViewController.h"
#import "IllegalQueryViewController.h"
#import "BeautyCollectionViewController.h"
#import "RadioListModel.h"
#import "DiscoveryHeadView.h"
#import "DiscoveryCollectionViewCell.h"
#import "DiscoveryMenuView.h"
#import "BlurImageView.h"
#import "BuyCarViewController.h"
#import "NewMapViewController.h"
#import "MenuTableViewCell.h"
@interface DiscoverViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) RadioListModel *radioSave;
@property (nonatomic, strong) DiscoveryMenuView *menu;
@property (nonatomic,strong) DiscoveryHeadView *headView;
@property (nonatomic,strong) UITableView *tableView;


@end

@implementation DiscoverViewController
/**
 *视图将要出现
 */
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
	[ProgressHUD dismiss];
	self.tabBarController.tabBar.hidden = NO;
	if (kRadioTool.isPlaying) {
		[self startRotationWithImageView];
		
	}else {
		[self.headView.rotationView.layer removeAllAnimations];
	}
	if (kRadioTool.radioModel) {
		self.headView.titleLablel.text = [NSString stringWithFormat:@"正在播出:%@ ", kRadioTool.radioModel.album];
	}
	
	
}
//播放旋转图
- (void)startRotationWithImageView{
 CABasicAnimation* rotationAnimation;
	rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
	rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
	rotationAnimation.duration = 6;
	rotationAnimation.cumulative = YES;
	rotationAnimation.repeatCount = MAXFLOAT;
	[self.headView.rotationView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
	
}


//初始化电台缩略
- (void)setHeaderView {
	self.headView = [[DiscoveryHeadView alloc]initWithFrame:CGRectMake(0 , 0, kWidth, 120)];
	self.headView.userInteractionEnabled = YES;
	self.headView.layer.shadowOffset = CGSizeMake(2, 2);
	self.headView.layer.shadowColor = [UIColor black50PercentColor].CGColor;
	self.headView.layer.shadowOpacity = 0.7;
	self.headView.layer.cornerRadius = 3;
	[self.headView.playButton addTarget:self action:@selector(enterRadioView) forControlEvents:(UIControlEventTouchUpInside)];
	self.tableView.tableHeaderView = self.headView;
}
//按钮点击
- (void)enterRadioView {
	
	CarRadioViewController *radio = [[CarRadioViewController alloc] init];
	[self.navigationController pushViewController:radio animated:YES];
	
}
/**
 *  初始化tableView
 */
- (void)initTable {
	self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)style:(UITableViewStyleGrouped)];
	self.view.backgroundColor = [UIColor  clearColor];
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
	self.tableView.separatorStyle = NO;
	[self.view addSubview:self.tableView];
	BlurImageView *blur = [[BlurImageView alloc]initWithFrame:kMainScreen];
	blur.image = [UIImage imageNamed:@"night.jpg"];
	blur.userInteractionEnabled = YES;
	self.tableView.backgroundView = blur;

}

- (void)viewDidLoad {
	[super viewDidLoad];
	self.title = @"发现";
	[self initTable];
	[self setHeaderView];


}
#pragma mark <-<-<-<------- tableViewDelegate------->->->->
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

	return 1;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

	return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 60;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 30;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString  *disCell = @"disCell";
	MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:disCell];
	if (!cell) {
		cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuTableViewCell" owner:nil options:nil] lastObject];
	}

	cell.backgroundColor = [UIColor clearColor];
	switch (indexPath.section) {
        case 0:
			[cell.imgView setImage:[UIImage imageNamed:@"modelicon"]];
			cell.titleLabel.text = @"媳妇当车模";
			return cell;
			break;
		case 1:
			[cell.imgView setImage:[UIImage imageNamed:@"location"]];
			cell.titleLabel.text = @"地图导航";
			return cell;
			break;
		case 2:
			[cell.imgView setImage:[UIImage imageNamed:@"calicon"]];
			cell.titleLabel.text = @"购车预算";
			return cell;
			break;
		case 3:
			[cell.imgView setImage:[UIImage imageNamed:@"illicon"]];
			cell.titleLabel.text = @"违章查询";
			return cell;
			break;
			
	}
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	switch (indexPath.section) {
		case 0:
		{
			/**
			 *  判断当前网络状态
			 */
			NSString *net = [self networkingStatesFromStatebar];
			if (![net isEqualToString:@"wifi"]) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"注意!" message:[NSString stringWithFormat:@"内容多图,当前正在使用%@网络",net] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
			[alert show];
			}else {
				BeautyCollectionViewController *beauty = [[BeautyCollectionViewController alloc] init];
				[self.navigationController pushViewController:beauty animated:YES];
			
			}
		}
			break;
		case 1:
		{
			
			NewMapViewController *map = [[NewMapViewController alloc] init];
			[self.navigationController pushViewController:map animated:YES];
		}
			break;
		case 2:
		{
			BuyCarViewController *buy = [[BuyCarViewController alloc] init];
			[self.navigationController pushViewController:buy animated:YES];
		}
			break;
		case 3:
		{
			IllegalQueryViewController *query = [[IllegalQueryViewController alloc] init];
			[self.navigationController pushViewController:query animated:YES];
			break;
	
		}
			
	}
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		BeautyCollectionViewController *beauty = [[BeautyCollectionViewController alloc] init];
		[self.navigationController pushViewController:beauty animated:YES];
	}
}
/**
 *  检测当前网络状态
 */
- (NSString *)networkingStatesFromStatebar {
	// 状态栏是由当前app控制的，首先获取当前app
	UIApplication *app = [UIApplication sharedApplication];
	
	NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
	
	int type = 0;
	for (id child in children) {
		if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
			type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
		}
	}
	NSString *stateString = @"wifi";
	
	switch (type) {
		case 0:
			stateString = @"notReachable";
			break;
			
		case 1:
			stateString = @"2G";
			break;
			
		case 2:
			stateString = @"3G";
			break;
			
		case 3:
			stateString = @"4G";
			break;
			
		case 4:
			stateString = @"LTE";
			break;
			
		case 5:
			stateString = @"wifi";
			break;
			
		default:
			break;
	}
	
	return stateString;
}
@end
